package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.IdCardPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GifSizeFilter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.MyGlideEngine;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IIdCardView;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.utils.FileProviderUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/19.
 * Desc   : 身份认证act
 */

public class IDCardRenzhengActivity extends BaseActivity<IdCardPresenter> implements IIdCardView {


    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idcard_number)
    EditText etIdcardNumber;
    @BindView(R.id.iv_front_side)
    ImageView ivFrontSide;
    @BindView(R.id.iv_back_side)
    ImageView ivBackSide;

    private static final int REQUEST_CODE_CHOOSE = 0x01;
    //0正面照,1背面照
    private String type;
    private String token;
    private String frontIdUrl;
    private String backIdUrl;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_idcard_renzheng);
        LoginInfoBean loginInfo = SharedPreferencesUtil.getsInstances(this).getLoginInfo();
        token = loginInfo.getToken();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new IdCardPresenter(this);
    }

    @Override
    protected void init() {
        setTopView();
    }

    private void setTopView() {
        txtTitle.setText("身份认证");
        imgLeft.setImageResource(R.mipmap.icon_left);
    }

    @OnClick({R.id.ll_leftview, R.id.iv_front_side, R.id.iv_back_side, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.iv_front_side:
                type = "0";
                Matisse.from(this)
                        .choose(MimeType.allOf())
                        .countable(true)
                        .maxSelectable(1)
                        .captureStrategy(new CaptureStrategy(true, FileProviderUtils.getFileProviderName(this)))
                        .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                        .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .thumbnailScale(0.85f)
                        .imageEngine(new MyGlideEngine())
                        .forResult(REQUEST_CODE_CHOOSE);
                break;
            case R.id.iv_back_side:
                type = "1";
                Matisse.from(this)
                        .choose(MimeType.allOf())
                        .countable(true)
                        .maxSelectable(1)
                        .captureStrategy(new CaptureStrategy(true, FileProviderUtils.getFileProviderName(this)))
                        .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                        .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .thumbnailScale(0.85f)
                        .imageEngine(new MyGlideEngine())
                        .forResult(REQUEST_CODE_CHOOSE);
                break;
            case R.id.btn_confirm:
                String idCardNumber = etIdcardNumber.getText().toString().trim();
                String name = etName.getText().toString().trim();
                if (frontIdUrl == null || backIdUrl == null || idCardNumber.equals("") || name.equals("")) {
                    ToastUtils.showToast("信息填写不全");
                } else {
                    mPresenter.authentic(token, name, idCardNumber, frontIdUrl, backIdUrl);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            List<Uri> uris = Matisse.obtainResult(data);
            String filePath = FileProviderUtils.getRealPathFromURI(this, uris.get(0));
            mPresenter.uploadPic(token, filePath);
        }
    }

    @Override
    public void uploadSuccess(String data) {
        ToastUtils.showToast("上传成功");
        if (type.equals("0")) {
            frontIdUrl = RetrofitSource.image_url + data;
            GlideImgManager.glideLoader(this, frontIdUrl, R.mipmap.img_error, R.mipmap.img_error, ivFrontSide);
        } else {
            backIdUrl = RetrofitSource.image_url + data;
            GlideImgManager.glideLoader(this, frontIdUrl, R.mipmap.img_error, R.mipmap.img_error, ivBackSide);
        }
    }

    @Override
    public void authenticSuccess(String data) {
        ToastUtils.showToast("认证成功");
        finish();
    }
}
